<?php
	session_start();
	require_once('../libraryfiles/dbconfig.php');
	require_once('../libraryfiles/DbTable/tbluser.php');
	require_once('../libraryfiles/function.php');
	require_once '../libraryfiles/SendEmail.php';
	$dbConn = mysql_connect ($dbhost, $dbuser, $dbpwd) or die ('MySQL connect failed. ' . mysql_error());
	mysql_select_db($dbname) or die('Cannot select database. ' . mysql_error());
	if(isset($_GET['confirmemail'])){
		$_SESSION['mails'] = $_GET['confirmemail'];
		$_SESSION['activated'] = $_GET['activated'];
	}
	if(isset($_POST['btnSubmit'])){
		$Password = $_POST['txtpassword'];
		$ConfirmPassword = $_POST['txtcomfirm'];
		if(empty($Password)){
			header("location:resetpassword.php?error=requiredpass&confirmemail=".$_SESSION['mails']."&activated=".$_SESSION['activated']);
			exit;
		}elseif(empty($ConfirmPassword)){
			header("location:resetpassword.php?error=requiredcon&confirmemail=".$_SESSION['mails']."&activated=".$_SESSION['activated']);
			exit;
		}elseif($Password != $ConfirmPassword){
			header("location:resetpassword.php?error=notequal&confirmemail=".$_SESSION['mails']."&activated=".$_SESSION['activated']);
			exit;
		}
		$query = "UPDATE ".Tbluser::Tbluser." SET ".Tbluser::user_pass."='".EnscryptPassword($Password)."' WHERE ".Tbluser::email."='".$_SESSION['mails']."' AND ".Tbluser::activate."='".$_SESSION['activated']."'";
		$updatesucess = mysql_query($query);
		if($updatesucess){
			unset($_SESSION['mails']);
			unset($_SESSION['activated']);
			header("location:login.php?activated=success");
			exit;
		}else{
			header("location:resetpassword.php?error=tryagain&confirmemail=".$_SESSION['mails']."&activated=".$_SESSION['activated']);
			exit;
		}
		
	}
	?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Reset Password</title>
<link href="css/stylesheet.css" rel="stylesheet" />
</head>

<div id="content">
  <div class="box" style="width: 400px; min-height: 300px; margin-top: 100px; margin-left: auto; margin-right: auto;">
    <div class="heading">
      <h1><img src="images/lockscreen.png" alt="" />Please enter your reset password details</h1>
    </div>
    <div class="content" style="min-height: 150px; overflow: hidden;">
       
      <form  method="post" name="frmreset" id="frmreset" action="resetpassword.php">
        <table style="width: 100%;">
          <tr>
              <td style="text-align: center;" rowspan="4"><img src="images/reset.png"  width="128" height="128" alt="Admin" /></td>
          </tr>
          <center>
          	<span style="color:red;">
		        <?php 
		        $getError = isset($_GET['error'])?$_GET['error']:"";
		        	switch ($getError){
		        		case "requiredpass":
		        			echo "Please provide password!";
		        			break;
		        		case "requiredcon":
		        			echo "Please provide confirm password!";
		        			break; 
		        		case "notequal":
		        			echo "Your password and confirm password are not matched!";
		        			break; 
		        		case "tryagain":
		        			echo "Please try again!";
		        			break;
		        	}
		        ?>
		        </span>
          </center>
          <tr>
         
            <td>Password<br />
            	<input name="txtpassword" id="txtpassword"  type="password" title="Password" value="" size="20" maxlength="2048" style="margin-top: 4px;" />
              
              <br />
              <br />
              Comform Password<br />
             	 <input type="password" name="txtcomfirm" id="txtcomfirm" title="Comfirm Pasword" value="" size="20" maxlength="2048" style="margin-top: 4px;" />
              
              </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td style="text-align: center;">
            <button type="submit" class="button" name="btnSubmit">Submit</button>
            </td>
          </tr>
        </table>
       
      </form>
  </div>
  </div>
</div>
